// pages/cart/cart.js

Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    goods: [{
        id: 1,
        name: "轻奢吧椅Y66",
        price: 8660.00,
        num: 1,
        imgurl: "/images/picgwc1.png"
      },
      {
        id: 2,
        name: "轻奢书椅Y69",
        price: 7520.00,
        num: 1,
        imgurl: "/images/picgwc2.png"
 
      }
    ],
    //所有商品的数
    goodscount: 0,
    is_edit: 0,
    //是否全选，s是编辑模式
    is_check_all_s: 0,
    is_check_all_d: 0,
    //结算时被选中的件数
    select_scount: 0,
    money: 0,
    num_input: 1
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let goods = this.data.goods
    let goodscount = 0
    for (let i = 0; i < goods.length; i++) {
      goodscount += goods[i].num
      //添加一个新的属性默认值为0,这个属性用来判断在编辑模式商品是否被选中
      goods[i].dot_s = 0
      //在非编辑模式下商品是否被选中
      goods[i].dot_d = 0
    }
    this.setData({
      goodscount: goodscount,
      goods: goods
    })
  },
  goshopping_func() {
    wx.navigateTo({
      url: '/pages/shop/shop',
    })
  },
  //改变购物车模式
  change_edit_func() {
    let is_edit = this.data.is_edit
    let goods = this.data.goods
    let is_check_all_s = this.data.is_check_all_s
    let is_check_all_d = this.data.is_check_all_d
    if (is_edit == 1) {
      for (let i = 0; i < goods.length; i++) {
        //添加一个新的属性默认值为0
        goods[i].dot_s = 0
      }
    } else {
      for (let i = 0; i < goods.length; i++) {
        //添加一个新的属性默认值为0
        goods[i].dot_d = 0
      }
    }
    this.setData({
      is_edit: !is_edit,
      goods: goods,
      is_check_all_s: 0,
      is_check_all_d: 0,
      //改变购物车模式时，所有选中商品数清零
      select_scount: 0,
      money: 0
    })
  },
  //商品在俩个模式中被选中时的处理
  select_goods_func(e) {
    let id = e.currentTarget.dataset.id
    let goods = this.data.goods
    let is_edit = this.data.is_edit
    if (is_edit) {
      for (let i = 0; i < goods.length; i++) {
        if (goods[i].id == id) {
          goods[i].dot_s = !goods[i].dot_s
        }
      }
    } else {
      for (let i = 0; i < goods.length; i++) {
        if (goods[i].id == id) {
          goods[i].dot_d = !goods[i].dot_d
        }
      }
      //非编辑模式下统计选中商品数量和总金额
      this.count_money(goods)
    }
    this.setData({
      goods: goods,
    })
  },
  //统计结算金额和结算商品数量
  count_money(goods) {
    let select_scount = 0
    let money = 0
    //在非编辑模式下每次选中商品重新统计被选中商品的件数
    for (let i = 0; i < goods.length; i++) {
      if (goods[i].dot_d) {
        select_scount += goods[i].num
        money += goods[i].num * goods[i].price
      }
    }
    this.setData({
      select_scount: select_scount,
      money: money
    })
  },
  //在不同模式下点击全选
  is_check_all_func() {
    let is_edit = this.data.is_edit
    let goods = this.data.goods
    let is_check_all_s = this.data.is_check_all_s
    let is_check_all_d = this.data.is_check_all_d
    if (is_edit) {
      if (is_check_all_s) {
        for (let i = 0; i < goods.length; i++) {
          goods[i].dot_s = 0
        }
      } else {
        for (let i = 0; i < goods.length; i++) {
          goods[i].dot_s = 1
        }
      }
      is_check_all_s = !is_check_all_s
    } else {
      if (is_check_all_d) {
        for (let i = 0; i < goods.length; i++) {
          goods[i].dot_d = 0
        }
      } else {
        for (let i = 0; i < goods.length; i++) {
          goods[i].dot_d = 1
        }
      }
      is_check_all_d = !is_check_all_d
      //在非编辑模式下每次全选商品重新统计被选中商品的件数
      this.count_money(goods)
    }
    this.setData({
      goods: goods,
      is_check_all_s: is_check_all_s,
      is_check_all_d: is_check_all_d,
    })
  },
  //用户点击删除按钮
  delete_func() {
    let that = this
    let goods = this.data.goods
    let goodscount = 0
    wx.showModal({
      title: '提示',
      content: '确认将已选中的删除吗？',
      success: function(res) {
        if (res.confirm) {
          goods = goods.filter(function(item) {
            return !item.dot_s
          });
          //删除时从小计算所有商品数
          for (let i = 0; i < goods.length; i++) {
            goodscount += goods[i].num
          }
          that.setData({
            goods: goods,
            goodscount: goodscount
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //购物车商品数量改变
  num_input_change_func(e) {
    let num_input = e.detail.value
    let id = e.currentTarget.dataset.id
    let goods = this.data.goods
    if (num_input.length == 1) {
      num_input = parseInt(num_input.replace(/[^1-9]/g, ''))
    } else if (num_input.length > 1) {
      num_input = parseInt(num_input.replace(/\D/g, ''))
    } else {
      num_input = 1
    }
    for (let i = 0; i < goods.length; i++) {
      if (goods[i].id == id) {
        goods[i].num = num_input
      }
    }
    //非编辑模式下统计选中商品数量和总金额
    this.count_money(goods)
  
  },
  reduce_func(e) {
    let id = e.currentTarget.dataset.id
    let goods = this.data.goods
    for (let i = 0; i < goods.length; i++) {
      if (goods[i].id == id) {
        if (goods[i].num > 1) {
          goods[i].num = goods[i].num - 1
        }
      }
    }
    //再重新统计
    this.count_money(goods)
    this.setData({
      goods: goods
    })
  },
  add_func(e) {
    let id = e.currentTarget.dataset.id
    let goods = this.data.goods
    for (let i = 0; i < goods.length; i++) {
      if (goods[i].id == id) {
        if (goods[i].num < 9999) {
          goods[i].num = goods[i].num + 1
        }
      }
    }
    //再重新统计
    this.count_money(goods)
    this.setData({
      goods: goods
    })
  },
 
})